package freezzz.server.EventDriver;


import com.friendfeed.api.EntryListResponse;
import com.friendfeed.api.Entry;
import com.friendfeed.api.internal.gson.GsonFactory;
import com.google.gson.Gson;
import freezzz.client.EventWidget.EventType;
import freezzz.client.EventWidget.TimelineEvent;
import freezzz.server.TestServiceImpl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.*;

public class DriverFriendFeedEvents extends EventsDriver {
    private com.friendfeed.api.FriendFeedService friendfeed;
    private final String USER = "freezzz";
    private final String KEY = "typo928aught";

    private static Hashtable<Date, ArrayList<TimelineEvent>> eventsCash = new Hashtable<Date, ArrayList<TimelineEvent>> ();

    public DriverFriendFeedEvents () {
        friendfeed = new com.friendfeed.api.FriendFeedServiceImpl ( USER, KEY );
        friendfeed.setUseCompression ( true );
        populateCache ();
    }

    private void populateCache () {
        boolean done = false;
        int start = 0;
        int num = 30;
        LinkedList<com.friendfeed.api.Entry> result = new LinkedList<Entry> ( );

        while( !done ){
            List<com.friendfeed.api.Entry> events = fetchUserFeed ( "freezzz", start, num );
            result.addAll ( events );
            start +=num;
            if( events.size () == 0 ) done =true;
        }
        System.out.println ("Fetched entryes num:" + result.size () );
        for ( com.friendfeed.api.Entry ent : result ) {
            Date createdDateKey = TestServiceImpl.getDayDate ( ent.getDate () );
            String iconUrl = "res/icons/friendfeed.png";
            String viaService = "Friendfeed";
            com.friendfeed.api.Via via = ent.getVia ();
            if ( via != null ) {
                viaService = via.getName ();
                if ( viaService.equals ( "Facebook" ) ) {
                    iconUrl = "res/icons/facebook.png";
                }
                if ( viaService.equals ( "Twitter" ) ) {
                    iconUrl = "res/icons/twitter.png";
                }
            }
            TimelineEvent tmpEvent = new TimelineEvent ( ent.getDate (), ent.getFrom ().getName (), ent.getBody (), new EventType ( viaService, iconUrl ) );

            if ( eventsCash.containsKey ( createdDateKey ) ) {
                if ( !eventsCash.get ( createdDateKey ).contains ( tmpEvent ) ) {
                    eventsCash.get ( createdDateKey ).add ( tmpEvent );
                }
            } else {
                eventsCash.put ( createdDateKey, new ArrayList<TimelineEvent> () );
                eventsCash.get ( createdDateKey ).add ( tmpEvent );
            }
        }

    }

    private List<com.friendfeed.api.Entry> fetchUserFeed ( String user, int start, int num ) {
        try {
            URL url = new URL ( "http://friendfeed-api.com/v2/feed/" + user +"?start="+start+"&num="+num);
            BufferedReader reader = new BufferedReader ( new InputStreamReader ( url.openStream () ) );
            String line;
            String responce = "";
            while ( ( line = reader.readLine () ) != null ) {
                responce = responce.concat ( line );
            }
            reader.close ();
            Gson gson = GsonFactory.createGson ();
            EntryListResponse r = gson.fromJson ( responce, EntryListResponse.class );
            return r.getEntries ();

        } catch ( Exception e ) {
            e.printStackTrace ();
        }
        return null;
    }

    public ArrayList<TimelineEvent> getEventsForDate ( Date date ) {
        ArrayList<TimelineEvent> result = new ArrayList<TimelineEvent> ();
        date = TestServiceImpl.getDayDate ( date );

        if ( eventsCash.containsKey ( date ) ) {
            return eventsCash.get ( date );
        }
        return result;
    }
}